<?php
  // Updated viewTranscript1.php
 if(!isset($_GET['sid'])) { ?>

<html>

<head>

<h2>View Transcript</h2>

<style>
  html {
        overflow: scroll; 
      }
.scrollable{
   overflow: auto;
   width: 70px; /* adjust this width depending to amount of text to display */
   height: 80px; /* adjust height depending on number of options to display */
   border: 1px silver solid;
 }
body{background-image:url('Images/sjsu logo.jpg');
      
background-repeat: no-repeat;
  background position: top center;
  background-size: 100%;
  background-size: 1400px 1200px;
  position:relative;}
  
h3.pos_fixed{
color:#6D63A9;
position:relative;
//top:25px;
//left:30px;

}

#b{
position:relative;
top:140px;
left:320px;
}


form {
position:relative;
top:140px;
left:320px;
}

table {
position:relative;
top:140px;
left:320px;
}

h2 {
position:relative;
top:140px;
left:320px;
}


a:link    {
  /* Applies to all unvisited links */
  text-decoration:  none;
  font-weight:      bold;
  color:          "#blue";
  font-size:120%;
  text-align: center;
  } 

</style>

</head>



<body>

<form action="viewTranscript1.php" method="GET">

Student Id: <input type='text' name = 'sid' /> <br/>

<input type='submit' name = 'view' value='View'/>

</form>

</body>

<br />

<div id="b"><a href="MainMenu.php">Home</a>

</html>

<?php } else {  ?>

<html>

<head>

<h2> Transcript</h2>

<style>
  html {
        overflow: scroll; 
      }
.scrollable{
   overflow: auto;
   width: 70px; /* adjust this width depending to amount of text to display */
   height: 80px; /* adjust height depending on number of options to display */
   border: 1px silver solid;
 }
body{background-image:url('Images/sjsu logo.jpg');
      
background-repeat: no-repeat;
  background position: top center;
  background-size: 100%;
  background-size: 1400px 1200px;
  position:relative;}
  
h3.pos_fixed{
color:#6D63A9;
position:relative;
//top:25px;
//left:30px;

}

table {
position:relative;
top:10px;
left:0px;
}

#b{
position:relative;
top:140px;
left:320px;
}


form {
position:relative;
top:140px;
left:320px;
}

h2 {
position:relative;
top:140px;
left:320px;
}


a:link    {
  /* Applies to all unvisited links */
  text-decoration:  none;
  font-weight:      bold;
  color:          "#blue";
  font-size:120%;
  text-align: center;
  } 

</style>

</head>



<body>

<div id="b"><b>Student Id:</b> <?php echo $_GET['sid'] ?> <br/>

<div id="c"><b>Cumulative GPA :</b> 


<?php


        require "config.php";

        define("CREDITS_PER_COURSE", 3);

        $grade_value = array("A" => 4.00,

                                                "A-" => 3.70, 

                                                "B+" => 3.33,

                                                "B" => 3.00,

                                                "B-" => 2.70,

                                                "C+" => 2.30,

                                                "C" => 2.00,

                                                "C-" => 1.70,

                                                "D+" => 1.30,

                                                "D" => 1.00,

                                                "D-" => 0.70 );


        function calculate_gpa($val){
                //echo " In calculate gpa";
                //echo "student id received : ". $val;

                global $grade_value;

                require 'config.php';

                $con = mysql_connect($db_host,$db_user,$db_pass);

                if (!$con){

                                die('Could not connect: ' . mysql_error());

                }

                mysql_select_db($db_name, $con);

                //calculating for courses enrolled in current term by student


                $sql_get = "SELECT * FROM enrollment where (studentId = '$val')" ;//for the current term calculate GPA
                $result = mysql_query($sql_get, $con);
                
                //echo " result : " . $result;
                $totalCredits = 0;

                $totalnumcourses = 0;

                while ($row = mysql_fetch_array($result)){

                        $grade = $row["grade"];
                        //echo "grade : " . $grade;  

                        if ( array_key_exists($grade, $grade_value)) {

                                $totalCredits += $grade_value[$grade] * CREDITS_PER_COURSE;

                                $totalnumcourses += CREDITS_PER_COURSE;

                        } 

                }

                $gpa = 0;

                if($totalnumcourses > 0) {

                        $gpa = $totalCredits/$totalnumcourses; // you may want to limit floating decimals

                         round($gpa, 3);

                }

                //echo $gpa;
                
                return $gpa;

        }



        
       
        //echo "Student id : " . $_GET['sid'];
        
        

        

        $studentid = $_GET['sid'];

        $con = mysql_connect($db_host,$db_user ,$db_pass);

        //if an error was encountered, the script exits with an error message

        if (!$con){

                die('Could not connect: ' . mysql_error());

        }

        //after successfully connecting with the database server, select the database with which you want to communicate

        mysql_select_db($db_name, $con);

        

        //code to print the transcript-with the current term taken from student table and course table as one variable

        

        //$sql_get = "select * from coursesection where (studentId = '$studentid')" ;

        //$result = mysql_query($sql_get);


        // echo "call calculate gpa with student id : " . $studentid;

        $calcugpa = calculate_gpa($studentid); //function to get the GPA
        
        echo round($calcugpa,2); ?>
        
<br>

<table border="1">
<tr>
 <td align=center><b>Student ID</b></td><td><b>Course ID</b></td><td><b>Course NAME</b></td><td><b>Course Grade</b></td><td><b>Course GPA</b></td><td align=center ><b>Term</b></td>
</tr>

<?php


   
   $con = mysql_connect($db_host,$db_user ,$db_pass);
   mysql_select_db($db_name, $con);

    $query = "SELECT `studentId`, `courseId`, `currentTerm`, `grade` FROM enrollment where (studentId = $studentid)";            
    $querycon = mysql_query($query,$con);
    while($row = mysql_fetch_row($querycon)){
        echo "<tr>";
        echo "<td align=center>".$row[0]."</td>"; //student id
        echo "<td align=center>".$row[1]."</td>"; //courseId
        
        $query1 = "SELECT `courseName` FROM `course` where (courseId = $row[1])";
        $querycon2 = mysql_query($query1,$con);
        $cname= mysql_fetch_row($querycon2); 
        echo "<td align=center>".$cname[0]."</td>"; //course Name
        echo "<td align=center>".$row[3]."</td>"; // Course Grade
        echo "<td align=center>".$grade_value[$row[3]]."</td>"; // Course GPA
        echo "<td align=center>".$row[2]."</td>"; //currentTerm 
        echo "</tr>";
    }

?>

</table>
<br />
<div id="d"><a href="MainMenu.php">Home</a>
</body>



</html>

 
<?php
        

  }

        

?>